/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import DAO.ChuDeDAO;
import DAO.WebsiteDAO;
import DAO.XpathDAO;
import DTO.ChuDeDTO;
import DTO.TinTucDTO;
import DTO.WebsiteDTO;
import DTO.XpathDTO;
import com.toedter.calendar.JTextFieldDateEditor;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author QuangHau
 */
public class Category extends javax.swing.JDialog {

    private int status;
    private int machude;
    public boolean isReset = false;

    /**
     * Creates new form Category
     */
    public Category(java.awt.Frame parent, boolean modal, int _status, int _machude) {
        super(parent, modal);
        initComponents();
        status = _status;
        machude = _machude;
        loadCmbWebsite();
        txtNgayBatDau.setDate(new Date(System.currentTimeMillis()));
        ((JTextFieldDateEditor)txtNgayBatDau.getDateEditor()).setEditable(false);
        if (status == 1 && machude != -1) {//cap nhat
            pnXpath.setVisible(false);
            this.setSize(this.getSize().width, this.getSize().height - pnXpath.getSize().height);
            btnThem.setText("Cập nhật");
            btnKiemTra.setVisible(false);
            this.setTitle("Cập nhật chủ đề");

            ChuDeDTO chudeDTO = ChuDeDAO.ThongTinChuDe(machude);
            txtTenChuDe.setText(chudeDTO.getTenChuDe());
            txtLink.setText(chudeDTO.getLinkChuDe());
            txtNgayBatDau.setDate(chudeDTO.getNgayBatDau());
            WebsiteDTO item = new WebsiteDTO();
            for (int i = 0; i < cmbWebsite.getItemCount(); i++) {
                item = (WebsiteDTO) cmbWebsite.getItemAt(i);
                if (item.getMaWebsite() == chudeDTO.getMaWebsite()) {
                    cmbWebsite.setSelectedIndex(i);
                    break;
                }
            }
        }
    }
//**************************************

    private void loadCmbWebsite() {
        ArrayList<WebsiteDTO> list = WebsiteDAO.SelectWebsite();
        Object[] items = new Object[list.size()];
        for (int i = 0; i < list.size(); i++) {
            items[i] = list.get(i);
        }
        cmbWebsite.setModel(new DefaultComboBoxModel(items));
    }

    private void checkEnable() {
        if (txtTenChuDe.getText().equals("")
                || txtLink.getText().equals("")
                || txtNgayBatDau.getCalendar().equals("")) {
            btnThem.setEnabled(false);
            btnKiemTra.setEnabled(false);
        } else {
            btnThem.setEnabled(true);
            if (txtTenXpath.getText().equals("")
                    || txtXpathTinTuc.getText().equals("")
                    || txtXpathLink.getText().equals("")
                    || txtXpathTieuDe.getText().equals("")) {
                btnKiemTra.setEnabled(false);
            } else {
                btnKiemTra.setEnabled(true);
            }
        }
    }
//***************************************    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtTenChuDe = new javax.swing.JTextField();
        cmbWebsite = new javax.swing.JComboBox();
        txtLink = new javax.swing.JTextField();
        txtNgayBatDau = new com.toedter.calendar.JDateChooser();
        pnXpath = new javax.swing.JPanel();
        txtXpathTinTuc = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        txtXpathTieuDe = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtXpathHinhAnh = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtXpathNgayDang = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtXpathMoTa = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtXpathPhanTrang = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtTenXpath = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        txtXpathLink = new javax.swing.JTextField();
        btnHuy = new javax.swing.JButton();
        btnKiemTra = new javax.swing.JButton();
        btnThem = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thêm Chủ đề");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Thông tin chủ đề"));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Tên chủ đề:");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Link:");

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Mã website:");

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("Ngày bắt đầu:");

        txtTenChuDe.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtTenChuDeCaretUpdate(evt);
            }
        });

        cmbWebsite.setMaximumSize(new java.awt.Dimension(2147483647, 2147483647));

        txtLink.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtLinkCaretUpdate(evt);
            }
        });

        txtNgayBatDau.setDateFormatString("dd/MM/yyyy");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtTenChuDe))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmbWebsite, 0, 135, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtLink, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE)
                    .addComponent(txtNgayBatDau, javax.swing.GroupLayout.DEFAULT_SIZE, 136, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel2, jLabel4});

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel3});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(txtTenChuDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtLink, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(cmbWebsite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNgayBatDau, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnXpath.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Thông tin xpath"));

        txtXpathTinTuc.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtXpathTinTucCaretUpdate(evt);
            }
        });

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Xpath tiêu đề:");

        txtXpathTieuDe.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtXpathTieuDeCaretUpdate(evt);
            }
        });

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("Xpath link:");

        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel7.setText("Xpath hình ảnh:");

        jLabel8.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel8.setText("Xpath ngày đăng:");

        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel9.setText("Xpath mô tả:");

        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel10.setText("Xpath phân trang:");

        jLabel11.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel11.setText("Tên xpath:");

        txtTenXpath.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtTenXpathCaretUpdate(evt);
            }
        });

        jLabel12.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel12.setText("Xpath tin tức:");

        txtXpathLink.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtXpathLinkCaretUpdate(evt);
            }
        });

        javax.swing.GroupLayout pnXpathLayout = new javax.swing.GroupLayout(pnXpath);
        pnXpath.setLayout(pnXpathLayout);
        pnXpathLayout.setHorizontalGroup(
            pnXpathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnXpathLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnXpathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel11, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnXpathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnXpathLayout.createSequentialGroup()
                        .addComponent(txtTenXpath)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel12))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnXpathLayout.createSequentialGroup()
                        .addComponent(txtXpathTieuDe)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnXpathLayout.createSequentialGroup()
                        .addComponent(txtXpathHinhAnh)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel8))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnXpathLayout.createSequentialGroup()
                        .addComponent(txtXpathMoTa)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnXpathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtXpathNgayDang)
                    .addComponent(txtXpathPhanTrang)
                    .addComponent(txtXpathLink)
                    .addComponent(txtXpathTinTuc))
                .addContainerGap())
        );

        pnXpathLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel10, jLabel12, jLabel6, jLabel8});

        pnXpathLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel11, jLabel5, jLabel7, jLabel9});

        pnXpathLayout.setVerticalGroup(
            pnXpathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnXpathLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnXpathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel11)
                    .addComponent(txtTenXpath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12)
                    .addComponent(txtXpathTinTuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnXpathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(txtXpathTieuDe, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtXpathLink, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnXpathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(txtXpathNgayDang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtXpathHinhAnh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnXpathLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(txtXpathPhanTrang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(txtXpathMoTa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnHuy.setText("Hủy");
        btnHuy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHuyActionPerformed(evt);
            }
        });

        btnKiemTra.setText("Kiểm tra");
        btnKiemTra.setToolTipText("");
        btnKiemTra.setEnabled(false);
        btnKiemTra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnKiemTraActionPerformed(evt);
            }
        });

        btnThem.setText("Thêm");
        btnThem.setEnabled(false);
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnXpath, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnKiemTra, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnThem)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnHuy)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnHuy, btnKiemTra, btnThem});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnXpath, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnKiemTra)
                    .addComponent(btnHuy)
                    .addComponent(btnThem))
                .addGap(23, 23, 23))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnKiemTraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnKiemTraActionPerformed
        XpathDTO xpathDTO = new XpathDTO();
        WebsiteDTO websiteDTO = (WebsiteDTO) cmbWebsite.getSelectedItem();
        xpathDTO.setTenChuDe(txtTenChuDe.getText());
        xpathDTO.setMaWebsite(websiteDTO.getMaWebsite());
        xpathDTO.setTenWebsite(websiteDTO.getTenWebsite());
        xpathDTO.setLinkChuDe(txtLink.getText());
        xpathDTO.setNgayBatDau(txtNgayBatDau.getDate());
        xpathDTO.setTenXpath(txtTenXpath.getText());
        xpathDTO.setLinkTieuDe(txtXpathLink.getText());
        xpathDTO.setTinTuc(txtXpathTinTuc.getText());
        xpathDTO.setTieuDe(txtXpathTieuDe.getText());
        xpathDTO.setHinhAnh(txtXpathHinhAnh.getText());
        xpathDTO.setNgayDang(txtXpathNgayDang.getText());
        xpathDTO.setMoTa(txtXpathMoTa.getText());
        xpathDTO.setTrang(txtXpathPhanTrang.getText());

        try {
            ArrayList<TinTucDTO> list = xpathDTO.getNewsOnePage(xpathDTO.getLinkChuDe());
            ArrayList<TinTucDTO> list2 = new ArrayList<TinTucDTO>();
            if (!xpathDTO.getPage().equals("") && !xpathDTO.getPage().equals(xpathDTO.getLinkChuDe())) {
                list2 = xpathDTO.getNewsOnePage(xpathDTO.getPage());
            } else {
                list2 = null;
            }
            Test test = new Test(null, true, new Object[]{list, list2}, 0);
            GUI.ShowCenterScreen(test);
            test.setVisible(true);
        } catch (Exception ex) {
            //Logger.getLogger(Xpath.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Không thể lấy tin.", "Lỗi", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btnKiemTraActionPerformed

    private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
        if (status ==0 && !btnKiemTra.isEnabled()) {
            int result = JOptionPane.showConfirmDialog(null, "Thêm chủ đề không có thông tin xpath?", "Thêm chủ đề", JOptionPane.YES_NO_OPTION);
            if (result != JOptionPane.YES_OPTION) {
                return;
            }
        }
        
        ChuDeDTO chudeDTO = new ChuDeDTO();
        chudeDTO.setLinkChuDe(txtLink.getText().trim());
        WebsiteDTO wsDTO = (WebsiteDTO) cmbWebsite.getSelectedItem();
        chudeDTO.setMaWebsite(wsDTO.getMaWebsite());
        chudeDTO.setTenChuDe(txtTenChuDe.getText().trim());
        chudeDTO.setNgayBatDau(txtNgayBatDau.getDate());
        
        if (status == 0) {
            if (ChuDeDAO.insertChuDe(chudeDTO)) {
                if (btnKiemTra.isEnabled()) {
                    XpathDTO xpathDTO = new XpathDTO();
                    xpathDTO.setMaChuDe(ChuDeDAO.maxID());
                    xpathDTO.setTenXpath(txtTenXpath.getText());
                    xpathDTO.setLinkTieuDe(txtXpathLink.getText());
                    xpathDTO.setTinTuc(txtXpathTinTuc.getText());
                    xpathDTO.setTieuDe(txtXpathTieuDe.getText());
                    xpathDTO.setHinhAnh(txtXpathHinhAnh.getText());
                    xpathDTO.setNgayDang(txtXpathNgayDang.getText());
                    xpathDTO.setMoTa(txtXpathMoTa.getText());
                    xpathDTO.setTrang(txtXpathPhanTrang.getText());

                    if (XpathDAO.insertXpath(xpathDTO)) {
                        JOptionPane.showMessageDialog(null, "Thêm Thành Công ", "Thông Tin", JOptionPane.INFORMATION_MESSAGE);
                        isReset = true;
                        this.dispose();
                    } else {
                        ChuDeDAO.deleteChuDe(ChuDeDAO.maxID());
                        JOptionPane.showMessageDialog(null, "Thêm thất bại", "Thông Tin", JOptionPane.INFORMATION_MESSAGE);
                    }
                }
                else {
                    JOptionPane.showMessageDialog(null, "Thêm Thành Công ", "Thông Tin", JOptionPane.INFORMATION_MESSAGE);
                    isReset = true;
                    this.dispose();
                }

            } else {
                JOptionPane.showMessageDialog(null, "Thêm thất bại", "Thông Tin", JOptionPane.INFORMATION_MESSAGE);
            }
        } else {
            chudeDTO.setMaChuDe(machude);
            if (ChuDeDAO.updateChuDe(chudeDTO)) {
                JOptionPane.showMessageDialog(null, "Cập nhật Thành Công", "Thông Tin", JOptionPane.INFORMATION_MESSAGE);
                isReset = true;
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(null, "Cập nhật thất bại", "Thông Tin", JOptionPane.INFORMATION_MESSAGE);
            }
        }
    }//GEN-LAST:event_btnThemActionPerformed

    private void btnHuyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHuyActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnHuyActionPerformed

    private void txtXpathTinTucCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtXpathTinTucCaretUpdate
        // TODO add your handling code here:
        checkEnable();
    }//GEN-LAST:event_txtXpathTinTucCaretUpdate

    private void txtXpathTieuDeCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtXpathTieuDeCaretUpdate
        // TODO add your handling code here:
        checkEnable();
    }//GEN-LAST:event_txtXpathTieuDeCaretUpdate

    private void txtTenChuDeCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtTenChuDeCaretUpdate
        checkEnable();
    }//GEN-LAST:event_txtTenChuDeCaretUpdate

    private void txtLinkCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtLinkCaretUpdate
        checkEnable();
    }//GEN-LAST:event_txtLinkCaretUpdate

    private void txtTenXpathCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtTenXpathCaretUpdate
        checkEnable();
    }//GEN-LAST:event_txtTenXpathCaretUpdate

    private void txtXpathLinkCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtXpathLinkCaretUpdate
        checkEnable();
    }//GEN-LAST:event_txtXpathLinkCaretUpdate

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Category.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Category.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Category.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Category.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Category dialog = new Category(new javax.swing.JFrame(), true, 0, 0);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHuy;
    private javax.swing.JButton btnKiemTra;
    private javax.swing.JButton btnThem;
    private javax.swing.JComboBox cmbWebsite;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel pnXpath;
    private javax.swing.JTextField txtLink;
    private com.toedter.calendar.JDateChooser txtNgayBatDau;
    private javax.swing.JTextField txtTenChuDe;
    private javax.swing.JTextField txtTenXpath;
    private javax.swing.JTextField txtXpathHinhAnh;
    private javax.swing.JTextField txtXpathLink;
    private javax.swing.JTextField txtXpathMoTa;
    private javax.swing.JTextField txtXpathNgayDang;
    private javax.swing.JTextField txtXpathPhanTrang;
    private javax.swing.JTextField txtXpathTieuDe;
    private javax.swing.JTextField txtXpathTinTuc;
    // End of variables declaration//GEN-END:variables
}
